from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from finallpro.items import FinallproItem,tmallItem

class TmallSpider(CrawlSpider):
    name = 'tmall'
    allowed_domains = ['tmall.com','detail.tmall.com']
    start_urls = ['http://www.tmall.com/','http://www.tmall.com/go/act/tmall/iwanttobuy.php']

    rules = (
		Rule(SgmlLinkExtractor(allow=(r'item.htm')),callback='tmall'),
		Rule(SgmlLinkExtractor(allow_domains=(r'list.tmall.com')))
    )

    def tmall(self,response):
		hxs=HtmlXPathSelector(response)
		i=tmallItem()
		i['type']='tmall'
		tmp=hxs.select("//strong[@id='J_StrPrice']/text()")
		if tmp:
			i['price']=tmp.extract()[0]
		else:
			i['price']=0
		tmp=hxs.select("//div[@class='order-number']/em/text()")
		if tmp:
			i['tid']=tmp.extract()[0]
		else:
			i['tid']='0000000'
		tmp=hxs.select("//img[@id='J_ImgBooth']/@src")
		if tmp:
			i['img']=tmp.extract()[0]
		else:
			i['img']='sdds'
		tmp=hxs.select("//div[@class=tb-detail-hd]/h3/text()")
		if tmp:
			i['title']=tmp.extract()[0]
		else:
			i['title']='dsdkjsksj'
		tmp=hxs.select("//ul[@class=attributes-list]/li/text()")
		subli=[]
		if tmp:
			for li in tmp:
				tt=li.extract()[0]
				subli.append(tt)
		i['tmp']=subli
		return i


